﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Room_Management
{
    public partial class fCreateContract : Form
    {
        private OleDbConnection myconn;
        private OleDbCommand cmd;
        int temp;
        string cbSelectItem;

        public fCreateContract()
        {
            cmd = new OleDbCommand();
            //myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""E:\Fpt University\C#\LearnCsharp\Room Management\Database\RoomManagement.mdb"";Persist Security Info=false");
            //this.myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\Users\claM\Desktop\LearningC#\Room Management\Database\RoomManagement.mdb"";Persist Security Info=false");
            myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""Database\RoomManagement.mdb"";Persist Security Info=false");
            cmd.Connection = myconn;
            this.myconn.Open();

            InitializeComponent();

            ComboBoxAddValue();
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            // Get Room ID
            if (cbRoom.Text == "Chọn Phòng:")
            {
                bSave.Enabled = false;
                MessageBox.Show("Hãy chọn phòng bạn muốn thanh toán!");
            }
            else 
            {
                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "INSERT INTO Customer(Name,Address,[Phone Number],CMND) VALUES ('" + this.tbName.Text + "','" + this.tbAddress.Text + "','" + this.tbPhoneNo.Text + "','" + this.tbCMNDNo.Text + "')";
                cmd.ExecuteNonQuery();

                // Get Customer ID
                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "SELECT MAX(ID) FROM Customer";
                string strCus = cmd.ExecuteScalar().ToString();

                cbSelectItem = cbRoom.Text;

                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "SELECT ID FROM Room WHERE RoomName ='" + cbSelectItem + "'";
                string strRoomid = cmd.ExecuteScalar().ToString();

                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "INSERT INTO Contract(CustomerID,RoomID,StartDate,ToDate,ContractDate) VALUES (" + Convert.ToInt32(strCus) + "," + Convert.ToInt32(strRoomid) + ",'" + this.dtpStartDate.Value + "','" + this.dtpToDate.Value + "','" + this.dtpContractDate.Value + "')";
                cmd.ExecuteNonQuery();


                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "UPDATE Room SET Status ='Bận' WHERE ID=" + Convert.ToInt32(strRoomid);
                temp = cmd.ExecuteNonQuery();
                if (temp > 0)
                {
                    MessageBox.Show("Tạo hợp đồng thành công!");
                    tbName.Clear();
                    tbAddress.Clear();
                    tbPhoneNo.Clear();
                    tbCMNDNo.Clear();

                    ComboBoxAddValue();
                }
                else
                {
                    MessageBox.Show("Tạo hợp đồng thất bại!");
                }
            }
        }

        public void ComboBoxAddValue()
        {
            cmd.CommandText = "SELECT * FROM Room WHERE Status ='Trống'";
            OleDbDataReader drRoom = cmd.ExecuteReader();
            ArrayList Rooms = new ArrayList();
            Rooms.Add(new AddValue("Chọn Phòng:", 0));
            while (drRoom.Read())
            {
                Rooms.Add(new AddValue(drRoom.GetString(1), drRoom.GetInt32(0)));
            }
            drRoom.Close();

            this.cbRoom.DataSource = Rooms;
            this.cbRoom.DisplayMember = "Display";
            this.cbRoom.ValueMember = "Value";
        }

        public class AddValue
        {
            private string m_Display;
            private long m_Value;
            public AddValue(string Display, long Value)
            {
                m_Display = Display;
                m_Value = Value;
            }
            public string Display
            {
                get { return m_Display; }
            }
            public long Value
            {
                get { return m_Value; }
            }
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            fMInterface fmain = new fMInterface();
            fmain.Show();
        }

        private void cbRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRoom.Text == "Chọn Phòng:")
            {
                bSave.Enabled = false;
                MessageBox.Show("Hãy chọn phòng bạn muốn thanh toán!");
            }
            else
            {
                bSave.Enabled = true;
            }
        }
    }
}
